package com.ups.singlearner.util;

import java.util.List;


public class NoteUtil {
	
	public enum Note {
		DO,
		RE,
		MI,
		FA,
		SOL,
		LA,
		SI
	}

	public static final float DO_VALUE  = 261.6f; 
	public static final float RE_VALUE  = 293.7f; 
	public static final float MI_VALUE  = 329.6f; 
	public static final float FA_VALUE  = 349.2f; 
	public static final float SOL_VALUE = 392.0f; 
	public static final float LA_VALUE  = 440.0f; 
	public static final float SI_VALUE  = 493.9f;
	
	public static Note getNoteFromFreq(float freq, float sigma) {
		if ((freq > DO_VALUE - sigma) && (freq < DO_VALUE + sigma)) {
			return Note.DO;
		}
		
		if ((freq > RE_VALUE - sigma) && (freq < RE_VALUE + sigma)) {
			return Note.RE;
		}
		
		if ((freq > MI_VALUE - sigma) && (freq < MI_VALUE + sigma)) {
			return Note.MI;
		}
		
		if ((freq > FA_VALUE - sigma) && (freq < FA_VALUE + sigma)) {
			return Note.FA;
		}
		
		if ((freq > SOL_VALUE - sigma) && (freq < SOL_VALUE + sigma)) {
			return Note.SOL;
		}
		
		if ((freq > LA_VALUE - sigma) && (freq < LA_VALUE + sigma)) {
			return Note.LA;
		}
		
		if ((freq > SI_VALUE - sigma) && (freq < SI_VALUE + sigma)) {
			return Note.SI;
		}
		
		return null;
	}
	
	public static float getFreqFromNote(Note n) {
		switch (n) {
		case DO:
			return DO_VALUE;
		case RE:
			return RE_VALUE;
		case MI:
			return MI_VALUE;
		case FA:
			return FA_VALUE;
		case SOL:
			return SOL_VALUE;
		case LA:
			return LA_VALUE;
		case SI:
			return SI_VALUE;
		default:
			return 0;
		}
	}
	
	public static int getIdx(Note n) {
		switch (n) {
		case DO:
			return 0;
		case RE:
			return 1;
		case MI:
			return 2;
		case FA:
			return 3;
		case SOL:
			return 4;
		case LA:
			return 5;
		case SI:
			return 6;
		default:
			return 99;
		}
		
	}
	
	public static int getResult(List<Note> reference, List<Note> sung) {
		if (reference.size() != sung.size()) {
			equalize(reference, sung);
		}
		
		int note = 0;
		for (int i = 0 ; i < reference.size() ; i++) {
			if (reference.get(i) == sung.get(i))
				note++;
		}
		
		return note;
	}
	
	private static void equalize(List<Note> reference, List<Note> sung) {
		//TODO if length of a note is different than record time, need to equalize ?
	}
}
